//
//  CHStringExtensions.m
//  POBase
//
//  Created by Pieter Omvlee on 3/9/08.
//  Copyright 2008 Bohemian Coding. All rights reserved.
//

#import "CHStringExtensions.h"


@implementation NSString (CHStringExtensions)

+ (NSString *)stringWithPathComponents:(NSArray *)components
{
  NSString *result = nil;
  for (NSString *comp in components) {
    if (!result) result = comp;
    else result = [result stringByAppendingPathComponent:comp];
  }
  return result;
}

- (NSString *)stringByPrefixingIfNessary:(NSString *)aString
{
  if ([self hasPrefix:aString])
    return self;
  return [aString stringByAppendingString:self];
}

- (NSString *)stringByDeletingPrefix:(NSString *)prefix
{
  NSRange r = [self rangeOfString:prefix];
  if (r.location == 0) {
    if (r.length == [self length])
      return @"";
    return [self substringFromIndex:r.length];
  }
  return self;
}

- (NSString *)stringByDeletingSuffix:(NSString *)suffix
{
  NSRange r = [self rangeOfString:suffix];
  if (r.location + r.length == [self length]) {
    if (r.length == [self length])
      return @"";
    return [self substringToIndex:r.location];
  }
  return self;
}

- (BOOL)startsWith:(NSString *)str
{
  NSRange r = [self rangeOfString:str];
  return r.location == 0;
}

- (BOOL)endsWith:(NSString *)str
{
  NSRange r = [self rangeOfString:str];
  return r.location == [self length]-r.length;
}

@end
